package me.firstapp.api.bbs;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.bbs.Advertisement;
import me.firstapp.service.bbs.AdvertisementService;

@Controller
@RequestMapping(value = "/advertisement")
@Api(value = "/advertisement", description = "广告相关接口")
public class AdvertisementApi extends BaseApi {

	@Autowired
	private AdvertisementService advertisementService;

	@RequestMapping(value = "findAdvertisementById.do", method = RequestMethod.GET)
	@ApiOperation(value = "根据id获取广告", notes = "根据id获取广告")
	public void findAdvertisementById(Long id, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Advertisement> result = new SingleObject<Advertisement>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Advertisement ad = advertisementService.findAdvertisementById(id, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(ad);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findAvailableAds.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取可用投放的广告", notes = "获取可用投放的广告")
	public void findAvailableAds(HttpServletRequest request, HttpServletResponse response) {
		ListObject<Advertisement> result = new ListObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			List<Advertisement> ads = advertisementService.findAvailableAds(apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setItems(ads);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findAdsPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询广告", notes = "分页查询广告")
	public void findAdsPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Advertisement> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<Advertisement> adPage = advertisementService.findAdvertisementPage(pageNo, pageSize, searchParams,
					sortParam, sortType, apiClientId);
			JsonPage<Advertisement> jsonPage = new JsonPage<Advertisement>();
			jsonPage.setList(adPage.getEntities());
			jsonPage.setPageNo(adPage.getPageNo());
			jsonPage.setPageSize(adPage.getPageSize());
			jsonPage.setTotalCount(adPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "addAdvertisement.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增广告", notes = "新增广告")
	public void addAdvertisement(@RequestBody Map<String, Object> body, Long startDate, Long endDate, Integer sort,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Advertisement> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			String title = (String) body.get("title");
			String description = (String) body.get("description");
			String image = (String) body.get("image");
			String url = (String) body.get("url");
			Advertisement ad = advertisementService.addAdvertisement(title, description, image, url, startDate, endDate,
					sort, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(ad);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "updateAdvertisement.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新广告", notes = "更新广告")
	public void updateAdvertisement(@RequestBody Map<String, Object> body, Long id, Long startDate, Long endDate,
			Integer status, Integer sort, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Advertisement> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			String title = (String) body.get("title");
			String description = (String) body.get("description");
			String image = (String) body.get("image");
			String url = (String) body.get("url");
			Advertisement ad = advertisementService.updateAdvertisement(id, title, description, image, url, startDate,
					endDate, status, sort, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(ad);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/deleteAdvertisements.do", method = RequestMethod.POST)
	@ApiOperation(value = "删除广告", notes = "删除广告")
	public void deleteAdvertisements(String ids, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			advertisementService.deleteAdvertisements(ids, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

}
